/**
 * EZCin : Easy Chinese Input by Romulus Ts'ai （蔡仲明）
 * 
 */
package idv.sais.ezcin;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.ClipboardManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class ezcin extends Activity {
	private static final int INPUT_NOTIFICATION = 0x1044;
	private Handler mHandler;
	private ArrayList<Button> candidate_buttons;

	static public EZCore gCore = null;
	static public EditText gText = null;
	static public TextView gIBox = null;
	static public ezcin gInstance;
	static public InputMode gKeyboardType = InputMode.EZ;

	private static String sBufferTitle_EZ = "";
	private static String sBufferTitle_BPMF = "";
	private static String sBufferTitle_ENG = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		gInstance = this;

		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.main);

		initHandler();
		initSoftKeys();
		setKeyButtonTexts();

		gCore = new EZCore(this, candidate_buttons);
		gText = (EditText) findViewById(R.id.text);
		gIBox = (TextView) findViewById(R.id.buffer);
		sBufferTitle_EZ = getString(R.string.buffer_label_ez);
		sBufferTitle_BPMF = getString(R.string.buffer_label_bpmf);
		sBufferTitle_ENG = getString(R.string.buffer_label_eng);

		final NotificationManager nm = (NotificationManager) this
				.getSystemService("notification");

		Notification n = new Notification(R.drawable.icon,
				getString(R.string.prepared), System.currentTimeMillis());
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, ezcin.class), 0);
		n.flags = Notification.FLAG_NO_CLEAR;
		n.setLatestEventInfo(this, getString(R.string.app_cname),
				getString(R.string.click_to_use), contentIntent);
		nm.notify(INPUT_NOTIFICATION, n);
	}

	public void onDestroy() {
		if (gCore != null) {
			gCore.closeDatabase();
		}
		super.onDestroy();
	}

	private void initSoftKeys() {
		candidate_buttons = new ArrayList<Button>();
		candidate_buttons.add((Button) findViewById(R.id.candidate_1));
		candidate_buttons.add((Button) findViewById(R.id.candidate_2));
		candidate_buttons.add((Button) findViewById(R.id.candidate_3));
		candidate_buttons.add((Button) findViewById(R.id.candidate_4));
		candidate_buttons.add((Button) findViewById(R.id.candidate_5));
		candidate_buttons.add((Button) findViewById(R.id.candidate_6));

		ArrayList<KeyTouchHandler> softkeys = new ArrayList<KeyTouchHandler>();
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_0_0), 0));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_0_1), 1));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_0_2), 2));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_0_3), 3));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_1_0), 4));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_1_1), 5));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_1_2), 6));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_1_3), 7));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_2_0), 8));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_2_1), 9));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_2_2), 10));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_2_3), 11));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_3_0), 12));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_space), 13));
		softkeys.add(new KeyTouchHandler(
				(ImageButton) findViewById(R.id.softkey_3_3), 14));

		for (int i = 0; i < EZCore.CANDIDATES_PER_PAGE; ++i) {
			final int idx = i;
			candidate_buttons.get(i).setOnClickListener(
					new Button.OnClickListener() {
						public void onClick(View v) {
							gCore.handleSelection(idx);
						}
					});
		}
	}

	private void setKeyButtonTexts() {
		((KeyButton) findViewById(R.id.softkey_3_3)).setText(new String[] {
				"BS", "輕", "注", "a4", "@" });

		switch (gKeyboardType) {
		case EZ:
			((KeyButton) findViewById(R.id.softkey_0_0)).setText(new String[] {
					"囗田", "厂广", "", "尸戶", "" });
			((KeyButton) findViewById(R.id.softkey_0_1)).setText(new String[] {
					"大", "竹", "寸", "八", "十" });
			((KeyButton) findViewById(R.id.softkey_0_2)).setText(new String[] {
					"水", "土士", "金", "木爪", "火" });
			((KeyButton) findViewById(R.id.softkey_0_3)).setText(new String[] {
					"冂月亅", "冖宀", "", "日門", "" });
			((KeyButton) findViewById(R.id.softkey_1_0)).setText(new String[] {
					"匚衣", "糸", "", "車", "" });
			((KeyButton) findViewById(R.id.softkey_1_1)).setText(new String[] {
					"女", "言", "山", "雨", "貝" });
			((KeyButton) findViewById(R.id.softkey_1_2)).setText(new String[] {
					"ク丿", "丶卜", "", "フ＼", "" });
			((KeyButton) findViewById(R.id.softkey_1_3)).setText(new String[] {
					"彐コ", "弓刁", "", "隹鳥", "" });
			((KeyButton) findViewById(R.id.softkey_2_0)).setText(new String[] {
					"廴辶", "艹川", "", "匈毋", "" });
			((KeyButton) findViewById(R.id.softkey_2_1)).setText(new String[] {
					"口", "耳卩", "心", "人入", "手" });
			((KeyButton) findViewById(R.id.softkey_2_2)).setText(new String[] {
					"一", "中虫", "丨", "ㄦ几", "厶" });
			((KeyButton) findViewById(R.id.softkey_2_3)).setText(new String[] {
					"Ｌ匕", "乂又", "", "戈弋", "" });
			((KeyButton) findViewById(R.id.softkey_3_0)).setText(new String[] {
					"的", "？", "。", "！", "，" });
			((KeyButton) findViewById(R.id.softkey_space))
					.setText(new String[] { "選字／空白", "上一頁", "", "下一頁", "" });
			break;
		case ENG:
			((KeyButton) findViewById(R.id.softkey_2_3)).setText(new String[] {
					"ａ", "ｂ", "ｃ", "ｄ", "ｅ" });
			((KeyButton) findViewById(R.id.softkey_2_2)).setText(new String[] {
					"ｆ", "ｇ", "ｈ", "ｉ", "ｊ" });
			((KeyButton) findViewById(R.id.softkey_2_1)).setText(new String[] {
					"ｋ", "ｌ", "ｍ", "ｎ", "ｏ" });
			((KeyButton) findViewById(R.id.softkey_2_0)).setText(new String[] {
					"ｐ", "ｑ", "ｒ", "ｓ", "ｔ" });
			((KeyButton) findViewById(R.id.softkey_1_3)).setText(new String[] {
					"ｕ", "ｖ", "ｗ", "ｘ", "ｙ" });
			((KeyButton) findViewById(R.id.softkey_1_1)).setText(new String[] {
					"０", "１", "２", "３", "４" });
			((KeyButton) findViewById(R.id.softkey_1_2)).setText(new String[] {
					"５", "６", "７", "８", "９" });
			((KeyButton) findViewById(R.id.softkey_0_0)).setText(new String[] {
					"Ａ", "Ｂ", "Ｃ", "Ｄ", "Ｅ" });
			((KeyButton) findViewById(R.id.softkey_0_1)).setText(new String[] {
					"Ｆ", "Ｇ", "Ｈ", "Ｉ", "Ｊ" });
			((KeyButton) findViewById(R.id.softkey_0_2)).setText(new String[] {
					"Ｋ", "Ｌ", "Ｍ", "Ｎ", "Ｏ" });
			((KeyButton) findViewById(R.id.softkey_0_3)).setText(new String[] {
					"Ｐ", "Ｑ", "Ｒ", "Ｓ", "Ｔ" });
			((KeyButton) findViewById(R.id.softkey_1_0)).setText(new String[] {
					"Ｕ", "Ｖ", "Ｗ", "Ｘ", "Ｙ" });
			((KeyButton) findViewById(R.id.softkey_3_0)).setText(new String[] {
					"，", "？", ".", "！", " '" });
			((KeyButton) findViewById(R.id.softkey_space))
					.setText(new String[] { "Space", "Ｚ", "", "ｚ", "" });
			break;
		case SYM:
			((KeyButton) findViewById(R.id.softkey_0_0)).setText(new String[] {
					"", "", "", "", "" });
			((KeyButton) findViewById(R.id.softkey_0_1)).setText(new String[] {
					"@", "#", "", "", "" });
			((KeyButton) findViewById(R.id.softkey_0_2)).setText(new String[] {
					"+", "-", "*", "/", "^" });
			((KeyButton) findViewById(R.id.softkey_0_3)).setText(new String[] {
					"$", "", "", "", "" });
			((KeyButton) findViewById(R.id.softkey_1_0)).setText(new String[] {
					"&", "", ">", "", "<" });
			((KeyButton) findViewById(R.id.softkey_1_1)).setText(new String[] {
					":", ";", ")", "", "(" });
			((KeyButton) findViewById(R.id.softkey_1_2)).setText(new String[] {
					"|", "", "]", "", "[" });
			((KeyButton) findViewById(R.id.softkey_1_3)).setText(new String[] {
					"~", "`", "}", "", "{" });
			((KeyButton) findViewById(R.id.softkey_2_0)).setText(new String[] {
					"/", "\\", ".", "", "" });
			((KeyButton) findViewById(R.id.softkey_2_1)).setText(new String[] {
					"\"", "'", "!", "?", "," });
			((KeyButton) findViewById(R.id.softkey_2_2)).setText(new String[] {
					"", "", "", "", "" });
			((KeyButton) findViewById(R.id.softkey_2_3)).setText(new String[] {
					"", "", "", "", "" });
			((KeyButton) findViewById(R.id.softkey_3_0)).setText(new String[] {
					"", "", "", "", "" });
			((KeyButton) findViewById(R.id.softkey_space))
					.setText(new String[] { "Space", "", "", "", "" });
			break;
		case BPMF:
			((KeyButton) findViewById(R.id.softkey_0_0)).setText(new String[] {
					"ㄅ", "ㄆ", "ㄇ", "ㄈ", "" });
			((KeyButton) findViewById(R.id.softkey_0_1)).setText(new String[] {
					"ㄉ", "ㄊ", "ㄋ", "ㄌ", "" });
			((KeyButton) findViewById(R.id.softkey_0_2)).setText(new String[] {
					"ㄍ", "ㄎ", "ㄏ", "", "" });
			((KeyButton) findViewById(R.id.softkey_0_3)).setText(new String[] {
					"ㄐ", "ㄑ", "", "ㄒ", "" });
			((KeyButton) findViewById(R.id.softkey_1_0)).setText(new String[] {
					"ㄓ", "ㄔ", "ㄕ", "ㄖ", "" });
			((KeyButton) findViewById(R.id.softkey_1_1)).setText(new String[] {
					"ㄗ", "ㄘ", "ㄙ", "", "" });
			((KeyButton) findViewById(R.id.softkey_1_2)).setText(new String[] {
					"ㄧ", "ㄨ", "ㄩ", "", "" });
			((KeyButton) findViewById(R.id.softkey_1_3)).setText(new String[] {
					"ㄚ", "ㄛ", "", "ㄜ", "ㄝ" });
			((KeyButton) findViewById(R.id.softkey_2_0)).setText(new String[] {
					"ㄞ", "ㄟ", "ㄠ", "ㄡ", "" });
			((KeyButton) findViewById(R.id.softkey_2_1)).setText(new String[] {
					"ㄢ", "ㄣ", "ㄤ", "ㄥ", "ㄦ" });
			((KeyButton) findViewById(R.id.softkey_2_2)).setText(new String[] {
					"一", "二", "三", "四", "輕" });
			((KeyButton) findViewById(R.id.softkey_2_3)).setText(new String[] {
					"", "", "", "", "" });
			((KeyButton) findViewById(R.id.softkey_3_0)).setText(new String[] {
					"的", "？", "。", "！", "，" });
			((KeyButton) findViewById(R.id.softkey_space))
					.setText(new String[] { "選字／空白", "上一頁", "", "下一頁", "" });
			break;
		}

	}

	public void updateKeyboard(String type) {
		gKeyboardType = InputMode.valueOf(type);
		mHandler.sendEmptyMessage(0);
	}

	static void setBufferText(CharSequence text) {
		switch (gKeyboardType) {
		case EZ:
			gIBox.setText(sBufferTitle_EZ + text);
			break;
		case BPMF:
			gIBox.setText(sBufferTitle_BPMF + text);
			break;
		case SYM:
		case ENG:
			gIBox.setText(sBufferTitle_ENG + text);
			break;
		}
	}

	// Handler Implementations

	private void initHandler() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					setKeyButtonTexts();
					break;
				}
			}
		};
	}

	// End of Handler Impl.

	// Menu Implementations

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(Menu.NONE, 0, Menu.NONE, getString(R.string.copy));
		menu.findItem(0).setIcon(R.drawable.menu_copy);
		menu.add(Menu.NONE, 1, Menu.NONE, getString(R.string.about));
		menu.findItem(1).setIcon(R.drawable.menu_about);
		menu.add(Menu.NONE, 2, Menu.NONE, getString(R.string.finish));
		menu.findItem(2).setIcon(R.drawable.menu_finish);

		return true;
	};

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			final ClipboardManager cm = (ClipboardManager) getSystemService("clipboard");
			final EditText txtBox = (EditText) findViewById(R.id.text);
			cm.setText(txtBox.getText());
			finish();
			return true;
		case 1:
			Intent aboutIntent = new Intent();
			aboutIntent.setClass(getApplicationContext(), AboutActivity.class);
			this.startActivity(aboutIntent);
			return true;
		case 2:
			final NotificationManager nm = (NotificationManager) this
					.getSystemService("notification");
			nm.cancel(INPUT_NOTIFICATION);
			finish();
			return true;
		}
		return false;
	}

	// End of Menu Impl.

}

enum InputMode {
	EZ,
	BPMF,
	ENG,
	SYM
}